<?php
/**
 * Dashboard Customizing API
 * 
 * Receive JSON string and handle dashboard update
 * 
 * @author Xavier VILAIN
 * @package Hive
 * @license
 * 
 */

use Hive\Widget;
$xview = Xview::instance();
$json = $xview->jsonPayload();
if(!empty($json)) {
	if(!empty($json['id'])) {
		$widgetid = $json['id'];
		try {
			$widget = Fox\Dao::load(Widget::className(),$widgetid);// Check if it's a valid WIdget
			// Load User Prefs
			$exists = false;
			$user = User::current();
 			$widgetPrefences = $user->getPrefs(User::PREFERENCE_WIDGETS);
			if(!empty($widgetPrefences)) {
				$preferences = json_decode($widgetPrefences,true);
				foreach($preferences as $key => $preference) {
					if(empty($preference['widgetid'])) {
						unset($preferences[$key]);
						continue;
					}
					$indexedPreferences[$preference['widgetid']] = $preference;
					if($preference['widgetid'] == $widgetid) {
						$exists = true;
						// Update Existing Preference
						if(!empty($json['attribute'])) {
							$attribute = $json['attribute'];
							if($attribute == 'remove') {
								unset($indexedPreferences[$preference['widgetid']]);
							} else {
								$indexedPreferences[$preference['widgetid']][$attribute] = isset($json[$attribute]) ? $json[$attribute] : null;
							}
							
						} elseif(!empty($json['position'])) {
							$oldPreference  = $preference;
							continue;
						}
					}
					$widgetList[$preference['column']][$preference['position']] = $preference;
				}
				if(!empty($indexedPreferences)) {
					$preferences = $indexedPreferences;
				}
				if(!empty($json['position'])) {
					
					list($column,$row) = explode(',',$json['position']);
					$oldColumn = $oldPreference['column'];
					$preferences[$oldPreference['widgetid']]['position'] = (int)$row;
					$preferences[$oldPreference['widgetid']]['column'] = (int)$column;
					$column = (int)$column;
					// ReOrder New Column
					if(!empty($widgetList[$column]) && count($widgetList[$column]) > 0) {
						$i= 0;
						$row = (int)$row;
						ksort($widgetList[$column]);
						foreach($widgetList[$column] as $index => $preference) {
							if($i == $row) {
								// At first position
								$i++;
							} 
							$preferences[$preference['widgetid']]['position'] = $i;
							$i++;
						}
					} 
					// ReOrder Old Column
					if(isset($widgetList[$oldColumn]) && $column != $oldColumn) {
						ksort($widgetList[$oldColumn]);
						$i= 0;
						foreach($widgetList[$oldColumn] as  $index => $preference) {
							$preferences[$preference['widgetid']]['position'] = $i;
							$i = $i+1;
						}
					}
				}
			} 
			if(!$exists) {
				$data = array();
				$widgetid = $json['id'];
				if(empty($preferences)) {
					$data = array('widgetid' => $widgetid,'column' => 1,'position' => 0);
				} else {
					$column = count($widgetList);
					$row = count($widgetList[$column]) + 2;
					$data = array('widgetid' => $widgetid,'column' => $column,'position' => $row);
				}
				if(!empty($json['attribute'])) {
					$attribute = $json['attribute'];
					$data[$attribute] = isset($json[$attribute]) ? $json[$attribute] : null;
				}
				$preferences[$widgetid] = $data;
			}
			if(!empty($preferences) && is_array($preferences)) {
				$preferences = json_encode($preferences);
			} else {
				$preferences = null;
			}
			$user->setPrefs(User::PREFERENCE_WIDGETS,$preferences);
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_SAVED_PREFERENCES));
		} catch(\Exception $e) {
			Fox\Message::Error($e);
		}
	}
}
echo $xview->messages(true,true);